#include "CGamePlayState.h"
#include "MainMenuState.h"
#include "Messaging\CSGD_MessageSystem.h"
#include "Messaging\CSGD_EventSystem.h"
#include "CPlayerTank.h"
#include "Game.h"
#include "CBullet.h"

CGamePlayState::CGamePlayState()
{
	m_pDInput		= NULL;
	m_pFMod			= NULL;
	m_pTexMan		= NULL;
	m_pObjFactory	= NULL;
	m_pObjManager	= NULL;

	m_bgMusicID	= -1;
	m_nSoundID1 = -1;

	screenSize.left = 0;
	screenSize.right = 0;
	screenSize.top = 0;
	screenSize.bottom = 0;
	m_pTank = NULL;
	m_pMS = NULL;
	m_pES = NULL;

	m_fFireTimer = 0.0f;

}

CGamePlayState::~CGamePlayState()
{

}

CGamePlayState* CGamePlayState::GetInstance()
{
	static CGamePlayState instance;
	return &instance;
}

void CGamePlayState::Enter(void)
{
	srand((unsigned int)GetTickCount());
	m_pObjFactory = CSGD_ObjectFactory<string,CBase>::GetInstance();
	m_pObjManager = CSGD_ObjectManager::GetInstance();
	m_pDInput = CSGD_DirectInput::GetInstance();
	m_pFMod	  = CSGD_FModManager::GetInstance();
	m_pTexMan = CSGD_TextureManager::GetInstance();

	m_pObjFactory->RegisterClassType<CPlayerTank>("CPlayerTank");
	m_pObjFactory->RegisterClassType<CTurret>("CTurret");
	m_pObjFactory->RegisterClassType<CBullet>("CBullet");

	m_pMS	= CSGD_MessageSystem::GetInstance();
	m_pMS->InitMessageSystem( CGamePlayState::MessageProc );
	m_pMS->SendMsg(new CCreateTurretMessage());
	m_pMS->SendMsg(new CCreateTurretMessage());

	m_pES	= CSGD_EventSystem::GetInstance();

	m_pTank = (CPlayerTank*)m_pObjFactory->CreateObject("CPlayerTank");
	m_pTank->SetPosX((float)(rand()%(CGame::GetInstance()->GetWindowWidth()-32)));
	m_pTank->SetPosY((float)(rand()%(CGame::GetInstance()->GetWindowHeight()-32)));
	m_bgMusicID = m_pFMod->LoadSound("resource/sound/game_music.ogg", FMOD_LOOP_NORMAL);
	m_pTank->SetTexID(CSGD_TextureManager::GetInstance()->LoadTexture("resource/graphics/PlayerTank.png", D3DCOLOR_ARGB(0,0,0,0)));

	//Tank Sounds
	int tankMoveSound = CSGD_FModManager::GetInstance()->LoadSound("resource/sound/Tank/Tiger_tracks.wav", FMOD_LOOP_NORMAL);
	int staticTankSound = CSGD_FModManager::GetInstance()->LoadSound("resource/sound/Tank/panzer_interior_ambience.ogg" , FMOD_LOOP_NORMAL);
	int tankShoot = CSGD_FModManager::GetInstance()->LoadSound("resource/sound/Tank/90mm_fire_1p.wav");
	m_nTurretImageID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/graphics/SGD_turret.png", D3DCOLOR_XRGB(255,255,255));
	m_nBulletImageID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/graphics/SGD_laser.bmp", D3DCOLOR_XRGB(255,255,255));
	m_nSoundID1 = CSGD_FModManager::GetInstance()->LoadSound("resource/sound/SGD_Lazershot.wav");
	
	m_pTank->SetInteriorSound(staticTankSound);
	m_pTank->SetMoveSound(tankMoveSound);
	m_pTank->SetShootSound(tankShoot);
	m_pFMod->SetVolume(tankShoot, 0.4f);
	m_pFMod->SetVolume(staticTankSound, 0.25f);
	m_pFMod->SetVolume(tankMoveSound, 0.25f);

	m_pObjManager->AddObject(m_pTank);
	m_pTank->Release();

	m_pFMod->SetVolume(m_bgMusicID, 2.0f);
	//m_pFMod->PlaySoundA(m_bgMusicID);
	m_nPlayerScore = 0;
	m_fTotalTime = 0;
}

bool CGamePlayState::Input(void)
{
	m_pDInput->ReadDevices();

	if (m_pDInput->KeyPressed(DIK_ESCAPE))
		CGame::GetInstance()->ChangeState(CMainMenuState::GetInstance());

	if (m_pDInput->KeyDown(DIK_LEFT))
	{
		
	}
	if (m_pDInput->KeyDown(DIK_RIGHT))
	{
		
	}

	return true;
}

void CGamePlayState::Update()
{
	CSGD_FModManager::GetInstance()->Update();
	m_pObjManager->UpdateObjects(CGame::GetInstance()->GetElapsedTime());
	m_pObjManager->CheckCollisions();
	m_fTotalTime += CGame::GetInstance()->GetElapsedTime();
	m_nPlayerScore = (int)m_fTotalTime * 10;

	m_fFireTimer += CGame::GetInstance()->GetElapsedTime();
	if(m_fFireTimer >= 1.0f)
	{
		m_pES->SendEvent("firebullet");
		m_fFireTimer = 0.0f;
	}

	m_pES->ProcessEvents();
	m_pMS->ProcessMessages();
}

void CGamePlayState::Render(void)
{
	screenSize.right = CGame::GetInstance()->GetWindowWidth();
	screenSize.bottom = CGame::GetInstance()->GetWindowHeight();


	m_pObjManager->RenderObjects();
	char buffer[128]= {0};
	sprintf_s(buffer, _countof(buffer), "Score:  %i", m_nPlayerScore);
	CSGD_Direct3D::GetInstance()->DrawTextA(buffer, 10, 0);
	CSGD_Direct3D::GetInstance()->GetSprite()->Flush();
}

void CGamePlayState::Exit(void)
{

	m_pES->ShutdownEventSystem();

	if (m_pMS)
	{
		m_pMS->ShutdownMessageSystem();
		m_pMS = NULL;
	}

	m_pFMod->StopSound(m_bgMusicID);

	CSGD_FModManager::GetInstance()->UnloadSound(m_bgMusicID);
	
	m_pObjManager->RemoveAllObjects();
	m_pObjManager->DeleteInstance();

	m_pObjFactory->ShutdownObjectFactory();
}

void CGamePlayState::HandleEvent(CEvent* pEvent)
{
	/*if (pEvent->GetEventID() == "power.increase")
	{
		float fPowRate = *((float*)pEvent->GetParam());

		m_fPower += fPowRate * m_fElapsedTime;
	}*/
}

void CGamePlayState::MessageProc(CBaseMessage* pMsg)
{
	switch(pMsg->GetMsgID())
	{
		case MSG_CREATE_TURRET:
		{
			CGamePlayState* pGamePlayState = CGamePlayState::GetInstance();
			CTurret* pTurret;
			pTurret = (CTurret*)pGamePlayState->m_pObjFactory->CreateObject("CTurret");
			pTurret->SetTarget(pGamePlayState->m_pTank);
			pTurret->SetPosX((float)(rand()%(CGame::GetInstance()->GetWindowWidth()-16)));
			pTurret->SetPosY((float)(rand()%(CGame::GetInstance()->GetWindowHeight()-16)));
			pTurret->SetTexID(pGamePlayState->m_nTurretImageID);
			pTurret->SetSoundID(pGamePlayState->m_nSoundID1);
			pGamePlayState->m_pObjManager->AddObject(pTurret);
			pTurret->Release();
		}
		break;

		case MSG_CREATE_BULLET:
		{
			CGamePlayState* pGamePlayState = CGamePlayState::GetInstance();
			CBullet* pBullet;
			
			pBullet = (CBullet*)pGamePlayState->m_pObjFactory->CreateObject("CBullet");

			CCreateBulletMessage* pCBM = (CCreateBulletMessage*)pMsg;
			pBullet->SetOwner(pCBM->GetTurret());

			pBullet->SetPosX(pBullet->GetOwner()->GetPosX());
			pBullet->SetPosY(pBullet->GetOwner()->GetPosY());
			pBullet->SetRotation(pCBM->GetTurret()->GetRotation());
			pBullet->SetTexID(pGamePlayState->m_nBulletImageID);

			tVector2D vecTurretAim;
			vecTurretAim.fX = 0.0f;
			vecTurretAim.fY = -1.0f;
			Vector2DRotate(vecTurretAim,pCBM->GetTurret()->GetRotation());
			pBullet->SetVelX(vecTurretAim.fX*0.1);
			pBullet->SetVelY(vecTurretAim.fY*0.1);

			pGamePlayState->m_pObjManager->AddObject(pBullet);
			pBullet->Release();
		}
		break;

		case MSG_DESTROY_TURRET:
		{
			CGamePlayState* pGamePlayState = CGamePlayState::GetInstance();
			CDestroyTurretMessage* pDTM = (CDestroyTurretMessage*)pMsg;
			pGamePlayState->m_pObjManager->RemoveObject(pDTM->GetTurret());
			pGamePlayState->m_pMS->SendMsg(new CCreateTurretMessage());
			pGamePlayState->m_nPlayerScore += 1000;
		}
		break;

		case MSG_DESTROY_BULLET:
		{
			CGamePlayState* pGamePlayState = CGamePlayState::GetInstance();
			CDestroyBulletMessage* pDBM = (CDestroyBulletMessage*)pMsg;
			pGamePlayState->m_pObjManager->RemoveObject(pDBM->GetBullet());
		}
		break;
	}
}